<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>日志管理</title>
    <link rel="stylesheet" th:href="@{/src/css/layui.css}">
</head>
<body>

<blockquote class="layui-elem-quote news_search">
    <form class="layui-form">
        <div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" id="username" value="" placeholder="请输入用户名"
                           class="layui-input search_input">
                </div>
            </div>
            <a class="layui-btn search_btn layui-btn-disabled"  shiro:lacksPermission="log:log:list">查询</a>
            <a class="layui-btn search_btn" lay-submit="" data-type="search"
                      lay-filter="search" shiro:hasPermission="log:log:list">查询</a>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-danger layui-btn-disabled"  shiro:lacksPermission="log:log:delete" >批量删除</a>
                <a class="layui-btn layui-btn-danger batchDel"  shiro:hasPermission="log:log:delete">批量删除</a>
            </div>
            <div class="layui-inline">（支持模糊查询）</div>
        </div>
    </form>
</blockquote>

    <table id="logs" lay-filter="test"></table>

<script type="text/html" id="barEdit">
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-disabled" lay-event="del" shiro:lacksPermission="log:log:delete" >删除</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" shiro:hasPermission="log:log:delete">删除</a>
</script>


    <script th:src="@{/src/layui.js}"></script>
    <script>
        //layui基于组件(模块)开发的     ctrl+shift+f9  工具中重构代码
        layui.use(['table','laydate','jquery','layer'],function () {
            var table = layui.table;
            var $ = layui.jquery;
            var layer = layui.layer;
            table.render({
                elem:'#logs',
                url:'/log/list',
                page:true,
                limit:5,
                limits:[5,10,15],
                cols:[
                    [
                        {field:'logId',type:'checkbox',width:40},
                        {field:'username',title:'用户名'},
                        {field:'operation',title:'操作名称'},
                        {field:'method',title:'方法'},
                        {field:'params',title:'参数'},
                        {field:'ip',title:'IP地址'},
                        {field:'createTime',title:'操作日期'},
                        {title:'操作',width:100,templet:'#barEdit'}
                    ]
                ],
                parseData:function (res) {
                    return {
                        "code":res.code==200?0:res.code,
                        "msg":res.message,
                        "count":res.data.total,
                        "data":res.data.list
                    }
                }
            })

            //搜索
            $(".search_btn").click(function () {
                table.reload('logs', {
                    url: '/log/list'
                    ,where: {
                        username: $("#username").val()
                    }

                });
            })
            //批量删除

            $(".batchDel").click(function () {
                var checkStatus = table.checkStatus("logs");
                //alert(checkStatus.data.length);
                var data = checkStatus.data;
                if(data.length == 0){
                    layer.msg("请选择数据",{icon:5,time:1000});
                    return ;
                }
                var keys = "";
                $.each(data,function (index,obj) {
                    keys += obj.logId+",";
                })
                keys = keys.substring(0,keys.length-1);
                layer.confirm('确认删除？',function (index) {
                    $.ajax({
                        url:'/log/deleteLog/'+keys,
                        type:'delete',
                        dataType:'json',
                        success:function (res) {
                            if(res.code == 200){
                                layer.msg('删除成功',{icon:1},function () {
                                    location.reload();
                                })
                            }else{
                                layer.msg('删除失败',{icon:5});
                            }
                        }
                    })
                    // layer.close(userList);
                })

            })

            
            table.on('tool(test)',function (obj) {
                var data = obj.data;//行数据
                var event = obj.event;
                if(event === 'del'){
                    layer.confirm('确认删除？',function (index) {
                        $.ajax({
                            url:'/log/delete/'+data.logId,
                            type:'delete',
                            dataType:'json',
                            success:function (res) {
                                if(res.code == 200){
                                    layer.msg('删除成功',{icon:1},function () {
                                        location.reload();
                                    })
                                }else{
                                    layer.msg('删除失败',{icon:5})
                                }
                            }
                        })

                        // layer.close(userList);
                    })
                }
            })

        })
    </script>

</body>
</html>